#!/bin/sh

prefix="/opt"
PATH=${prefix}/bin:${prefix}/sbin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=amavisd
DAEMON=${prefix}/sbin/${NAME}
DAEMON_OPTS="-c ${prefix}/etc/${NAME}.conf"
PIDFILE=${prefix}/var/spool/amavis/${NAME}.pid

test -x $DAEMON || exit 0

if [ -z "$1" ] ; then
    case `echo "$0" | sed 's:^.*/\(.*\):\1:g'` in
        S??*) rc="start" ;;
        K??*) rc="stop" ;;
        *) rc="usage" ;;
    esac
else
    rc="$1"
fi

case "$rc" in
    start)
    	if [ -e $PIDFILE ]; then
    	  if [ -z "$(pidof $NAME| grep $(cat $PIDFILE))" ]; then
    	    rm -rf $PIDFILE
    	  else
    	    echo "$NAME already started!"
    	    exit 0
    	  fi
    	fi
    	
	echo "Starting server: $NAME"
	$DAEMON $DAEMON_OPTS
        ;;
    stop)
        if [ -n "$(pidof $NAME)" ]; then
            echo "Stopping server: $NAME"
            killall $NAME 2> /dev/null
        fi
        ;;
    restart)
        "$0" stop
        sleep 1
        "$0" start
        ;;
    *)  
        echo "Usage: $0 (start|stop|restart|usage)"
        ;;
esac

exit 0
